package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.model.dto.ProcessMaterialsDto;
import eteam.aps.model.dto.ProcessPreserveDto;
import eteam.aps.model.vo.moco_rders.MocoRdersDisplayVo;
import eteam.aps.model.vo.process_materials.ProcessMaterialsShowVo;
import eteam.aps.model.vo.process_materials.ProcessMaterialsVo;
import eteam.aps.service.MocoRdersService;
import eteam.aps.service.ProcessMaterialsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工艺物料 控制层
 *
 * @author LiuLei
 */
@Api(tags = "工艺物料")
@RestController
@RequestMapping("pmc")
public class ProcessMaterialsController {

    @Autowired
    private ProcessMaterialsService processMaterialsService;

    @Autowired
    private MocoRdersService mocoRdersService;

    @PostMapping("/bmo")
    @ApiOperation("根据工单代号、任务号获取产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invCode", value = "产品编号"),
            @ApiImplicitParam(name = "mocCode", value = "工单代号")
    })
    public R<List<MocoRdersDisplayVo>> getByMoList(
            @RequestParam(value = "invCode", required = false) String invCode,
            @RequestParam(value = "mocCode", required = false) String mocCode) throws Exception {
        if (Helper.isEmpty(invCode) && Helper.isEmpty(mocCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号、工单代号,不能为空!");
        return R.success(mocoRdersService.getByMcDetauils(invCode, mocCode));
    }

    @PostMapping("/pmbiom")
    @ApiOperation("根据工单代号和任务号查询工序维护列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invCode", value = "产品编号"),
            @ApiImplicitParam(name = "mocCode", value = "工单代号")
    })
    public R<ProcessMaterialsShowVo> getPMByInvOrMoc(
            @RequestParam(value = "invCode", required = false) String invCode,
            @RequestParam(value = "mocCode", required = false) String mocCode) {
        if (Helper.isEmpty(invCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号,不能为空!");
        if (Helper.isEmpty(mocCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "工单代号,不能为空!");
        return R.success(processMaterialsService.getPMByInvOrMoc(invCode, mocCode));
    }

    @PostMapping("/pmbi")
    @ApiOperation("根据产品编号查询工序维护列表")
    @ApiImplicitParam(name = "invCode", value = "产品编号")
    public R<List<ProcessMaterialsVo>> getPMByInv(@RequestParam(value = "invCode", required = false) String invCode) {
        if (Helper.isEmpty(invCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号,不能为空!");
        return R.success(processMaterialsService.getPMByInv(invCode));
    }

    @PostMapping("/pmbm")
    @ApiOperation("根据制令号查询工单维护列表")
    @ApiImplicitParam(name = "mocCode", value = "任务令")
    public R<List<ProcessMaterialsVo>> getPMByMoc(String mocCode) {
        if (Helper.isEmpty(mocCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "任务令,不能为空!");
        return R.success(processMaterialsService.getPMByMoc(mocCode));
    }

    @PostMapping("/rm")
    @ApiOperation("删除任务工序标准")
    @ApiImplicitParam(name = "ids", value = "内码（多个用，隔开）")
    public R<Integer> rmMocInfo(String ids) {
        if (Helper.isEmpty(ids)) throw new BusinessException(HttpStatus.BAD_REQUEST, "制令编号不能为空！");
        return R.success(processMaterialsService.rmMocInfo(ids));
    }

    @PostMapping("/ri")
    @ApiOperation("删除产品工序标准")
    @ApiImplicitParam(name = "ids", value = "内码（多个用，隔开）")
    public R<Integer> rmInvInfo(String ids) {
        if (Helper.isEmpty(ids)) throw new BusinessException(HttpStatus.BAD_REQUEST, "制产品编号不能为空！");
        return R.success(processMaterialsService.rmInvInfo(ids));
    }

    @PostMapping("/mmi")
    @ApiOperation("修改制令工序标准")
    @ApiImplicitParam(name = "ProcessPreserveDto", value = "工单工艺维护")
    public R<Integer> modMocInfo(@RequestBody ProcessPreserveDto processPreserveDto) throws Exception {
        if (processPreserveDto == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "非法参数异常！");
        if (Helper.isEmpty(processPreserveDto.getId()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "制令内码不能为空！");
        return R.success(processMaterialsService.modMocInfo(processPreserveDto));
    }

    @PostMapping("/mii")
    @ApiOperation("修改产品工序标准")
    @ApiImplicitParam(name = "ProcessPreserveDto", value = "工单工艺维护")
    public R<Integer> modInvInfo(@RequestBody ProcessPreserveDto processPreserveDto) throws Exception {
        if (processPreserveDto == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "非法参数异常！");
        if (Helper.isEmpty(processPreserveDto.getId()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品内码不能为空！");
        return R.success(processMaterialsService.modInvInfo(processPreserveDto));
    }

    @PostMapping("/wmi")
    @ApiOperation("重写任务令")
    @ApiImplicitParam(name = "ProcessMaterialsShowVo", value = "工艺维护")
    public R<ProcessMaterialsShowVo> reWriteMocInfo(@RequestBody ProcessMaterialsDto pmd) throws Exception {
        if (Helper.isEmpty(pmd.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        if (Helper.isEmpty(pmd.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "任务令不能为空！");
        if (pmd.getProcessMaterialsList() == null || pmd.getProcessMaterialsPreserveList().isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常");
        return R.success(processMaterialsService.reWriteMocInfo(pmd));
    }

    @PostMapping("/wii")
    @ApiOperation("重写产品")
    @ApiImplicitParam(name = "ProcessMaterialsShowVo", value = "工艺维护")
    public R<ProcessMaterialsShowVo> reWriteInvInfo(@RequestBody ProcessMaterialsDto pmd) throws Exception {
        if (Helper.isEmpty(pmd.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        if (Helper.isEmpty(pmd.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "任务令不能为空！");
        if (pmd.getProcessMaterialsList().isEmpty() || pmd.getProcessMaterialsPreserveList() == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常");
        return R.success(processMaterialsService.reWriteInvInfo(pmd));
    }
}
